<template>
  <div class="container">
    <scroll-view scroll-y
                 style="height:100%">
      <div class="box">
        <p class="sear">
          <input type="text"
                 @confirm="search"
                 confirm-type="search"
                 class="inp"
                 v-model="search_content"
                 :disabled="isshare"
                 maxlength="11"
                 placeholder="请输入推荐码或手机号"
                 placeholder-style="color:#CCCCCC;" />
          <img v-if="img_baseurl && !search_content &&!isshare"
               @click="saoma"
               :src="img_baseurl+'/mp_3.0.2/login_ic_scan.png'"
               alt />
          <img v-if="img_baseurl && search_content &&!isshare"
               @click="clearfun"
               :src="img_baseurl+'/mp_3.0.2/login_ic_delete.png'"
               alt />
        </p>
        <p class="nocade"
           v-if="!search_content">
          <span @click="nocode">暂无推荐码</span>
        </p>
        <div class="resule"
             v-if="search_content">
          <div class="head">
            <img class="headimg"
                 v-if="param.headimgurl"
                 :src="'https://img.ameimeika.com/'+param.headimgurl"
                 alt />
            <img class="isv"
                 v-if="param.is_vip"
                 src="https://img.ameimeika.com/h5_images/mp_images/mp_3.0/mine/ic_member@3x.png"
                 alt />
          </div>
          <div class="info">
            <p class="tit">{{param.nickname}}</p>
            <p class="desp">推荐您加入美亿齿</p>
          </div>
        </div>
      </div>
      <div class="enter"
           @click="enteryfun">进入美亿齿</div>
    </scroll-view>
  </div>
</template>
<script>
import common from "@/assets/js/mmk_common.js";
export default {
  data () {
    return {
      img_baseurl: common.image_resource,
      super_code: "",
      imgurl: common.image_resource + "/mp_3.0.2/bg_referral@2x.png",
      search_content: "",
      param: {},
      message: "",
      isshare: false
    };
  },
  watch: {
    search_content (val) {
      if (!val) {
        this.param = {};
      }
    }
  },
  methods: {
    bind_user () {
      //分享过来的绑定上下级  旧的api/v4_5/user/share
      let that = this;
      let s_data = wx.getStorageSync("s_data");
      let user_id = wx.getStorageSync("user_id");
      common.fly_post("api/act/share/bind_invite", {
        sid: s_data.sid || 0,
        activity_id: s_data.tg || 0
      }, result => {

      })
      common.fly_post(
        "api/v4_5/super_code/write",
        {
          user_id: user_id - 0,
          code: that.search_content ? that.search_content : "AAAAAA",
          source: s_data.d || 13,
          is_new: 1,
          activity_id: s_data.tg || 0
        },
        response => {
          console.log(response);
          if (response.data.status_code == 0) {
            let s_data = wx.getStorageSync("s_data");
            if (s_data.t == 35) {
              // 邀请有礼专属
              let s_data = wx.getStorageSync('s_data');
              wx.showLoading({
                title: "", //提示的内容,
                mask: true, //显示透明蒙层，防止触摸穿透,
                success: res => { }
              });
              common.fly_post("api/act/activity/detail", {
                activity_id: s_data.tg || 105
              }, result => {
                let res = result.data
                wx.hideLoading();
                if (res.status_code == 0) {
                  wx.redirectTo({ url: '/pages/package_main/invitation_latest/main?act_id=' + s_data.tg })
                  //   if (res.data.is_join_old == 0) {
                  //     wx.redirectTo({ url: '/pages/package_main/invitation_old/main?act_id=' + s_data.tg });
                  //   } else {
                  //     wx.redirectTo({ url: '/pages/package_main/invitation_latest/main?act_id=' + s_data.tg });
                  //   }
                }
              })
            } else {
              wx.getStorage({
                key: "jump_url",
                success: result => {
                  console.log(result.data);
                  wx.reLaunch({ url: result.data });
                  // wx.redirectTo({ url: result.data });
                  wx.removeStorage({
                    key: "jump_url",
                    success: res => { }
                  });
                },
                fail: () => {
                  //   绑定成功  返回到上一级
                  setTimeout(() => {
                    wx.navigateBack({
                      delta: 2 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
                    });
                  }, 1500);
                },
                complete: () => { }
              });
            }

          } else {
            wx.showToast({
              title: "绑定失败",
              icon: "none",
              duration: 2000
            });
          }
          // wx.navigateBack({
          //   delta: 2 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
          // });
        }
      );
    },
    nocode () {
      //暂无推荐码
      this.search_content = "AAAAAA";
      this.search();
    },
    search () {
      //搜索事件查询详情(根据super_code码或手机号 显示用户信息（汪攀）)
      common.fly_post(
        "api/v4_5/super_code/get_detail",
        {
          super_code: this.search_content
        },
        result => {
          let res = result.data;
          wx.hideLoading();
          if (res.status_code == 0) {
            console.log(res);
            this.param = res.data;
          } else {
            res.message = res.message ? res.message : "该信息不存在";
            wx.showToast({
              title: res.message,
              icon: "none",
              duration: 2000
            });
          }
        }
      );
    },
    clearfun () {
      //清除搜索
      this.search_content = "";
    },
    detail () {
      //POST 根据sid显示用户推荐码（汪攀）能查到推荐码的话锁死
      wx.showLoading({
        title: "加载中", //提示的内容,
        mask: true //显示透明蒙层，防止触摸穿透,
      });
      let s_data = wx.getStorageSync("s_data");
      common.fly_post(
        "api/v4_5/super_code/get_code",
        {
          sid: s_data.sid ? s_data.sid - 0 : 0,
          activity_id: s_data.tg || 0
        },
        result => {
          console.log(result);
          let res = result.data;
          wx.hideLoading();
          if (res.status_code == 0) {
            this.search_content = res.data.super_code;
            console.log(1111, this.search_content);
            if (this.search_content) {
              this.search();
            }
          } else {
            wx.showToast({
              title: "该信息不存在",
              icon: "none",
              duration: 2000
            });
          }
        }
      );
    },
    loginfun () {
      //登陆
      // wx.setStorageSync("loginobj", objset);
      console.log(this.login);
      let s_data = wx.getStorageSync("s_data");
      this.login.source = s_data.d || 13
      common.fly_post("api/v4_5/minapp/bind_mobile", this.login, response => {
        let res = response.data;
        wx.hideLoading();
        if (res.status_code == 0) {
          wx.setStorageSync("user_token", res.data.token);
          wx.setStorageSync("user_id", res.data.user_id);
          wx.setStorageSync("is_plus", res.data.user.data.is_plus);
          wx.setStorageSync("account_type", res.data.user.data.account_type);
          wx.setStorageSync("role_id", res.data.user.data.role_id);
          wx.setStorageSync(
            "is_home_plus_entrance",
            res.data.user.data.is_home_plus_entrance
          );
          this.bind_user();
          wx.showToast({
            title: "登录成功", //提示的内容,
            icon: "success", //图标,
            duration: 1500, //延迟时间,
            mask: true, //显示透明蒙层，防止触摸穿透,
            success: res => { }
          });
        } else {
          wx.showToast({
            title: res.message, //提示的内容,
            icon: "none", //图标,
            duration: 1500, //延迟时间,
            mask: false //显示透明蒙层，防止触摸穿透,
          });
        }
      });
    },
    enteryfun () {
      //加入美美
      /**分享过来要跳转到相对应的页面的方法 */
      // if (!this.search_content) {
      //   wx.showToast({
      //     title: "推荐不能为空",
      //     icon: "none",
      //     duration: 2000
      //   });
      //   return false;
      // }
      const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
      if (this.search_content || !this.search_content) {
        if (this.search_content.length < 6 || this.search_content.length > 11) {
          wx.showToast({
            title: "请输入正确的推荐码或手机号",
            icon: "none",
            duration: 2000
          });
          return false;
        } else {
          common.fly_post(
            "api/v4_5/super_code/get_detail",
            {
              super_code: this.search_content
            },
            result => {
              let res = result.data;
              wx.hideLoading();
              if (res.status_code == 0) {
                console.log(res);
                this.param = res.data;
                if (this.login.phone == this.param.username) {
                  wx.showToast({
                    title: "请输入正确的推荐码或手机号",
                    icon: "none",
                    duration: 2000
                  });
                } else {
                  this.loginfun();
                }
                // this.loginfun();
              } else {
                res.message = res.message ? res.message : "该信息不存在";
                wx.showToast({
                  title: res.message,
                  icon: "none",
                  duration: 2000
                });
              }
            }
          );
        }
      }
    },
    saoma () {
      /**扫描 */
      var _this = this;
      // 允许从相机和相册扫码
      wx.scanCode({
        success: res => {
          var result = res.result;

          _this.search_content = result;
          console.log(_this.search_content);
          this.search();
        }
      });
    }
  },

  onLoad (options) {
    wx.setNavigationBarTitle({
      title: "推荐码"
    });
    this.super_code = options.super_code;
    let s_data = wx.getStorageSync("s_data");
    // this.isshare = s_data.sid ? true : false;
    this.detail();
  },
  onShow () {
    this.login = wx.getStorageSync("loginobj");
    this.detail();
  }
};
</script>

<style scoped lang="less">
.container {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  background-color: #ffffff;
  img {
    width: 100%;
    height: auto;
  }
  .box {
    background: #f7f7f7;
    margin: 30px 15px;
    border-radius: 10px;
    padding: 15px;
    .sear {
      background: #ffffff;
      position: relative;
      border-radius: 10px;
      input {
        height: 36px;
        line-height: 36px;
        font-size: 16px;
        padding: 5px;
      }
      img {
        width: 20px;
        height: 20px;
        position: absolute;
        right: 10px;
        top: 50%;
        margin-top: -10px;
        z-index: 999;
      }
    }
    .nocade {
      margin: 29px 0px 15px;
      text-align: right;
      span {
        padding: 5px 10px;
        line-height: 30px;
        color: #ff383e;
        font-size: 14px;
        background: rgba(255, 56, 62, 0.1);
        border-radius: 30px;
      }
    }
  }
  .enter {
    width: 255px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    background: linear-gradient(
      45deg,
      rgba(228, 0, 98, 1) 0%,
      rgba(240, 139, 56, 1) 89%
    );
    box-shadow: 0px 4px 20px 0px rgba(234, 72, 76, 1);
    border-radius: 16px;
    font-size: 18px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: rgba(255, 255, 255, 1);
    line-height: 44px;
    margin: auto;
    position: fixed;
    bottom: 50px;
    left: 50%;
    margin-left: -127px;
  }
  .resule {
    display: flex;
    margin-top: 15px;
    .head {
      position: relative;
      .headimg {
        width: 44px;
        height: 44px;
        border-radius: 50%;
      }
      .isv {
        position: absolute;
        right: -3%;
        bottom: 12%;
        width: 32rpx;
        height: 32rpx;
      }
    }
    .info {
      padding-left: 10px;
      .tit {
        font-size: 16px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: rgba(51, 51, 51, 1);
        line-height: 22px;
        margin-bottom: 8rpx;
      }
      .desp {
        font-weight: 400;
        color: rgba(255, 56, 62, 1);
        font-size: 12px;
      }
    }
  }
}
</style>

